//
//  ConfirmOrderCell.m
//  MobileShoppingMall
//
//  Created by 冯传昌 on 2021/4/30.
//  Copyright © 2021 冯传昌. All rights reserved.
//

#import "ConfirmOrderCell.h"

#import "SecondConfirmOrderCell.h"

@interface ConfirmOrderCell()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@property (nonatomic,strong) UIView *bgView;

@property (nonatomic,strong) UIImageView *logoImgV;

@property (nonatomic,strong) UILabel *nameLB;

@property (nonatomic,strong) UIButton *toDetailBtn;

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) NSMutableArray *dataSourceArr;

@property (nonatomic,strong) UILabel *leftLB;//左侧留言LB

@property (nonatomic,strong) UITextField *leave_TF;//留言

@end

@implementation ConfirmOrderCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = MainBackColor;
        self.backgroundColor = MainBackColor;
        
        self.dataSourceArr = [NSMutableArray array];
        
        [self initUI];
    }
    return self;
}
#pragma mark - setter
- (void)setModel:(ConfirmOrderModel *)model{
    _model = model;
    
    self.bgView.height = _model.cellHeight - 12;
    self.tableView.height = _model.cellHeight - 112;
    
    self.leftLB.top = self.tableView.bottom + 15;
    self.leave_TF.top = self.tableView.bottom + 10;
    
    self.nameLB.text = _model.company;
    self.nameLB.width = [Configuration getLabelWidthWithText:self.nameLB.text withFont:self.nameLB.font];
    self.toDetailBtn.left = self.nameLB.right + 5;
    
    self.leave_TF.text = _model.leaveMessage;
    
    self.dataSourceArr = [NSMutableArray arrayWithArray:_model.products];
    
    [self.tableView reloadData];
}

#pragma mark - UI
-(void)initUI{
    [self.contentView addSubview:self.bgView];
    [self.bgView addSubview:self.logoImgV];
    [self.bgView addSubview:self.nameLB];
    [self.bgView addSubview:self.toDetailBtn];
    [self.bgView addSubview:self.tableView];
    [self.bgView addSubview:self.leftLB];
    [self.bgView addSubview:self.leave_TF];
}
#pragma mark - lazy
- (UIView *)bgView{
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 210)];
        _bgView.clipsToBounds = YES;
        _bgView.layer.cornerRadius = 8;
        _bgView.backgroundColor = UIColor.whiteColor;
    }
    return _bgView;
}
- (UIImageView *)logoImgV{
    if (!_logoImgV) {
        _logoImgV = [[UIImageView alloc] initWithFrame:CGRectMake(16, 17, 15, 15)];
        _logoImgV.image = LoadImage(@"商品详情_店铺");
    }
    return _logoImgV;
}

- (UILabel *)nameLB{
    if (!_nameLB) {
        _nameLB = [[UILabel alloc] initWithFrame:CGRectMake(self.logoImgV.right + 4, self.logoImgV.top - 2, 80, 20)];
        _nameLB.textColor = MainBlackTextColor;
        _nameLB.font = FontSet(15);
        _nameLB.textAlignment = NSTextAlignmentLeft;
        
        _nameLB.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(toStoreClick:)];
        tap.delegate = self;
        [_nameLB addGestureRecognizer:tap];
    }
    return _nameLB;
}
- (UIButton *)toDetailBtn{
    if (!_toDetailBtn) {
        _toDetailBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _toDetailBtn.frame = CGRectMake(self.nameLB.right + 5, self.nameLB.top, 40, 20);
        [_toDetailBtn setImage:LoadImage(@"个人中心_查看全部") forState:normal];
        [_toDetailBtn setImageEdgeInsets:UIEdgeInsetsMake(5, 0, 5, 34)];
        [_toDetailBtn addTarget:self action:@selector(toDetailClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _toDetailBtn;
}
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 50, ScreenWidth, 110) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = MainBackColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollEnabled = NO;
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 0.1f)];
        [_tableView setTableHeaderView:view];
    }
    return _tableView;
}

- (UILabel *)leftLB{
    if (!_leftLB) {
        _leftLB = [[UILabel alloc] initWithFrame:CGRectMake(15, self.tableView.bottom + 15, 45, 20)];
        _leftLB.textColor = MainBlackTextColor;
        _leftLB.font = FontSet(14);
        _leftLB.text = @"留言";
        _leftLB.textAlignment = NSTextAlignmentLeft;
    }
    return _leftLB;
}
- (UITextField *)leave_TF{
    if (!_leave_TF) {
        _leave_TF = [[UITextField alloc] initWithFrame:CGRectMake(73, self.tableView.bottom + 10, ScreenWidth - 73 - 15, 30)];
        _leave_TF.clipsToBounds = YES;
        _leave_TF.layer.cornerRadius = 4;
        _leave_TF.backgroundColor = MainBackColor;
        _leave_TF.placeholder = @" 选填，请先与卖家协商一致，不超过120个字";
        _leave_TF.font = FontSet(12);
        _leave_TF.delegate = self;
    }
    return _leave_TF;
}

#pragma mark - UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSourceArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 110;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    SecondConfirmOrderCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SecondConfirmOrderCell"];
    if (!cell) {
        cell = [[SecondConfirmOrderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"SecondConfirmOrderCell"];
    }
    cell.model = self.dataSourceArr[indexPath.row];
    return cell;
}
#pragma mark - SEL
//进入店铺
-(void)toStoreClick:(UIGestureRecognizer *)tap{
    [self toDetailClick];
}
//进入店铺
-(void)toDetailClick{
}
#pragma mark - UITextFieldDelegate
- (void)textFieldDidEndEditing:(UITextField *)textField{
    self.model.leaveMessage = textField.text;
    if (self.updateMessageBlock) {
        self.updateMessageBlock(self.model);
    }
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSInteger maxLength = 120;//设置限制字数
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if (toBeString.length > maxLength && range.length!=1){
        textField.text = [toBeString substringToIndex:maxLength];
        [textField resignFirstResponder];
        return NO;
    }
    return YES;
}



@end
